package co.edu.usbcali.pqr.dataaccess.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import co.edu.usbcali.pqr.dataaccess.api.HibernateDaoImpl;
import co.edu.usbcali.pqr.modelo.SolicitudPqr;
import co.edu.usbcali.pqr.modelo.dto.ConsultaSolicitudDTO;


/**
 * A data access object (DAO) providing persistence and search support for
 * SolicitudPqr entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @see lidis.SolicitudPqr
 */
@Scope("singleton")
@Repository("SolicitudPqrDAO")
public class SolicitudPqrDAO extends HibernateDaoImpl<SolicitudPqr, Long>
    implements ISolicitudPqrDAO {
    @SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SolicitudPqrDAO.class);
    @Resource
    private SessionFactory sessionFactory;

    public static ISolicitudPqrDAO getFromApplicationContext(
        ApplicationContext ctx) {
        return (ISolicitudPqrDAO) ctx.getBean("SolicitudPqrDAO");
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<ConsultaSolicitudDTO> consultaSolicitud(Long numeroRadicacion,
			Long idEstado, Long idTipoSol, Long idMotSol, Long idMotRecl, Date fechaInicio, Date fechaFin) throws Exception {
            
		List<ConsultaSolicitudDTO> list = null;
		
		try {
			SimpleDateFormat formateador = new SimpleDateFormat("dd/MM/yyyy");		
			
			Query query = getSession().getNamedQuery("buscar_solicitudes_rango_fechas_estado_numeroradicacion");
			query.setParameter("pNumeroR", numeroRadicacion);
			query.setParameter("pIdEstado", idEstado);
			query.setParameter("pIdTipoSol", idTipoSol);
			//query.setParameter("pIdMotSol", idMotSol);
			//query.setParameter("pIdMotRec", idMotRecl);
			query.setParameter("pFechaInicio",formateador.format(fechaInicio));
			query.setParameter("pFechaFin", formateador.format(fechaFin));
			query.setResultTransformer(Transformers.aliasToBean(ConsultaSolicitudDTO.class));
			list = query.list();
			
		} catch (Exception e) {
			throw e;
		}
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ConsultaSolicitudDTO> consultaSolicitudxArea(Long idArea)
			throws Exception {
		List<ConsultaSolicitudDTO> list = null;
		
		try {
			
			Query query = getSession().getNamedQuery("consultar_solicitudes_x_id_area");
			query.setParameter("pIdArea", idArea);
			query.setResultTransformer(Transformers.aliasToBean(ConsultaSolicitudDTO.class));
			list = query.list();
			
		} catch (Exception e) {
			throw e;
		}
		
		return list;
	}
    
    
}
